package top.txl.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.txl.entity.AddressBook;
import top.txl.entity.OrderDetail;
import top.txl.entity.Orders;
import top.txl.entity.ShoppingCart;
import top.txl.exception.CustomerException;
import top.txl.mapper.*;
import top.txl.service.OrderDetailService;
import top.txl.service.OrderService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public void save(Orders order) {
        // 判断用户购物车是否有数据
        Integer userId = order.getUserId();
        List<ShoppingCart> cartList = shoppingCartMapper.getCartLists(userId);
        if(cartList == null || cartList.size() == 0){
            throw new CustomerException("购物车数据为空不能下单");
        }


        // 判断用户是否有选择地址
        Integer addressBookId = order.getAddressBookId();
        AddressBook bookItem = addressBookMapper.getBookDetail(userId, addressBookId);
        if(addressBookId == null){
            throw new CustomerException("收货地址为空不能下单");
        }



        // 生成订单号
        long number = IdWorker.getId();

        AtomicInteger amount = new AtomicInteger(0);

        //计算金额，和组装订单菜品数据
        List<OrderDetail> ods  = cartList.stream().map(item ->{
            OrderDetail od = new OrderDetail();
            od.setAmount(item.getAmount());
            od.setNumber(item.getNumber());
            od.setOrderId(String.valueOf(number));
            od.setDishFlavor(item.getDishFlavor());
            od.setDishId(item.getDishId());
            od.setImage(item.getImage());
            od.setSetmealId(item.getSetmealId());
            od.setName(item.getName());
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return od;
        }).collect(Collectors.toList());

        // 判断是否有支付方式
        if(order.getPayMethod() != null){
            order.setPayMethod(order.getPayMethod());
        }

        // 组装订单数据
        order.setNumber(String.valueOf(number));
        order.setOrderTime(LocalDateTime.now());
        order.setAmount(new BigDecimal(amount.get())); // 总金额
        order.setCheckoutTime(LocalDateTime.now());
        order.setConsignee(bookItem.getConsignee());
        order.setPhone(bookItem.getPhone());
        order.setUserId(userId);
        order.setUserName(bookItem.getConsignee());
        order.setAddress(bookItem.getDetail() != null ? bookItem.getDetail() : "");

        orderMapper.save(order);

        orderDetailMapper.save(ods);

        //清空购物车数据
        shoppingCartMapper.deletedAllCartListsByUserId(userId);
    }
}
